<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>03_排序数组查找</title>
</head>
<body>
    
</body>
</html>

<script>
    /**
     * 统计一个数字在排好序的数组中出现的次数
    */

   let tempArr = [2,4,4,4,5,5,5,5,6,8,8,8,8,8,8,9,9,9];
   let newArr = [];
   function getArrNumCount (arr) {
       if(!arr || arr.length<1) return;
       //统计每个数据的个数
       arr.forEach((item,index) => {
            // 如果数组是排好序的
            if(newArr.length > 0 && newArr[newArr.length-1].num === item) {
                newArr[newArr.length-1].count++;
            }else {
                newArr.push({'num':item,'count':1})
            }
       })
       let newArrTest = JSON.parse(JSON.stringify(newArr));
        //根据count得出出现次数最多的数字 根据count排序
        newArr.forEach((item,index) => {
            if(index === newArr.length-1) return;
            let nextNum = newArr[index+1];
            if(item.count > nextNum.count) {
                let temp = item;
                newArr[index] = nextNum;
                newArr[index+1]= temp;
            }
        })
        return newArr[newArr.length-1];
   }
   console.log(getArrNumCount(tempArr))

</script>